﻿using FluentValidation;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.WarehouseInsp.Buz.Msg;
using System;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.Core;
using FluentValidation.Results;
using Microhard.WarehouseInsp.Buz.BuzEnum;

namespace Microhard.WarehouseInsp.Buz.Validator
{
    public class OutIntPutWarehouseValidator : AbstractValidator<OutIntPutWarehouseModel>
    {
        private IOutIntPutWarehouse _iipw = IoC.Resolve<IOutIntPutWarehouse>();

        public OutIntPutWarehouseValidator()
        {
            RuleSet("SaveChangePrice", () =>
            {
                Custom(u =>
                {
                    if (u.OutInPutWarehouseIDForSelect == Guid.Empty)
                    {
                        return new ValidationFailure("ProductName", OutIntputWarehouse.NeedOIWID);
                    }
                    return null;
                });
                RuleFor(u => u.OutInPrice).GreaterThan(0).WithMessage(OutIntputWarehouse.NeedChangePrice).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
            });

            RuleSet("Insert", () =>
            {
                RuleFor(u => u.OutInCount).GreaterThan(0).WithMessage(OutIntputWarehouse.NeedOutInCount).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                Custom(u =>
                {
                    //出库申请的时候，申请出库金额不能大于库存金额
                    if (u.OutInPutType == (int)OutInPutType.Out)
                    {
                        //质押物是否重复添加
                        if (_iipw.HasAddPlg(Guid.Parse(u.InProductIDForSelect), u.OutInPutApplyID))
                        {
                            return new ValidationFailure("OutInPutWarehouseID", "DuplicatePlg");
                        }
                        double amount;
                        if (u.OutInAmount <= 0)
                        {
                            if (u.OutInPrice <= 0)
                            {
                                return new ValidationFailure("OutInPutWarehouseID", "ZeroAmount");
                            }
                            amount = (double)u.OutInPrice * u.OutInCount;
                        }
                        else
                        {
                            amount = (double)u.OutInAmount;
                        }

                        if (_iipw.IsOutCount(Guid.Parse(u.InProductIDForSelect), amount))
                        {
                            return new ValidationFailure("OutInPutWarehouseID", "OutInCountIsBigger");
                        }
                    }
                    if (u.OutInPutType == (int)OutInPutType.In)
                    {
                        if (u.PlnPdgID == Guid.Empty)
                        {
                            return new ValidationFailure("ProductName", OutIntputWarehouse.NeedOIWID);
                        }
                        if (u.OutInPrice <= 0 && u.OutInAmount <= 0)
                        {
                            return new ValidationFailure("OutInAmount", OutIntputWarehouse.NeedOutInAmount);
                        }
                    }
                    return null;

                });
            });

            RuleSet("Update", () =>
            {
                RuleFor(u => u.OutInCount).GreaterThan(0).WithMessage(OutIntputWarehouse.NeedOutInCount);


                Custom(u =>
                {
                    if (u.OutInPutType == (int)OutInPutType.Out)
                    {
                        var old = _iipw.GetByOutInPutWarehouseID(u.OutInPutWarehouseID);
                        //列表添加中质押物是否重复添加
                        if (!string.IsNullOrEmpty(u.InProductIDForSelect))
                        {
                            var newplgID = Guid.Parse(u.InProductIDForSelect);
                            if (old.PlnPdgID != newplgID)
                            {
                                if (_iipw.HasAddPlg(newplgID, u.OutInPutApplyID))
                                {
                                    return new ValidationFailure("OutInPutWarehouseID", "DuplicatePlg");
                                }
                            }
                        }
                        double amount;
                        if (u.OutInAmount <= 0)
                        {
                            if (u.OutInPrice <= 0)
                            {
                                return new ValidationFailure("OutInPutWarehouseID", "ZeroAmount");
                            }
                            amount = (double)u.OutInPrice * u.OutInCount;
                        }
                        else
                        {
                            amount = (double)u.OutInAmount;
                        }
                        //出库申请的时候，申请出库金额不能大于库存金额
                        if (_iipw.IsOutCount(u.PlnPdgID, amount))
                        {
                            return new ValidationFailure("PlnPdgID", SubProjectMsg.IsApproving);
                        }
                        if (u.Status == (int)PrjStatus.Finished)
                        {
                            //出库登记的时候，实际出库金额不能大于申请出库金额                    
                            if (old != null)
                            {
                                if (amount > (double)old.OutInAmount)
                                {
                                    return new ValidationFailure("PlnPdgID", "OutInCountIsBigger");
                                }
                            }
                        }
                    }
                    if (u.OutInPutType == (int)OutInPutType.In)
                    {
                        if (u.PlnPdgID == Guid.Empty)
                        {
                            return new ValidationFailure("ProductName", OutIntputWarehouse.NeedOIWID);
                        }
                        if (u.OutInPrice <= 0 && u.OutInAmount <= 0)
                        {
                            return new ValidationFailure("OutInAmount", OutIntputWarehouse.NeedOutInAmount);
                        }
                    }
                    return null;
                });
            });
        }

    }
}
